package org.example;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Result1 {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //带有返回值
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum=0;
                for (int i = 0; i < 1000; i++) {
                    sum+=i;
                }
                return sum;
            }
        };
        FutureTask<Integer> futureTask=new FutureTask<>(callable);
        //创建线程, 线程的构造⽅法传⼊ FutureTask . 此时新线程就会执⾏ FutureTask 内部的 Callable 的
        //call ⽅法, 完成计算. 计算结果就放到了 FutureTask 对象中
        Thread t = new Thread(futureTask);
        t.start();
        int result=futureTask.get();
        System.out.println(result);
    }


}
